package net.receiving.tcp;

import javax.net.ServerSocketFactory;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.InetAddress;
import java.util.concurrent.BlockingQueue;
import java.util.List;
import java.util.Map;
import java.io.IOException;

import util.Config;

/**
 * Created by HREN_VAM.
 */
public class TCPServer implements Runnable {

    private ServerSocket socket;
    private final BlockingQueue<Map.Entry<InetAddress, List<Byte>>> queue;

    public TCPServer(BlockingQueue<Map.Entry<InetAddress, List<Byte>>> queue) {
        socket = null;
        try {
            socket = ServerSocketFactory.getDefault().createServerSocket(Config.getInstance().getReceivePort());
        } catch (IOException e) {
            e.printStackTrace();
        }
        this.queue = queue;
    }

    public void run() {
        try {
            while (true) {
                Socket client = socket.accept();
                new Thread(new TCPConnection(client, queue)).start();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
